/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.kwickkiosk.services.dao;

import com.kwickkiosk.infrastructure.exception.EPlatformDatabaseException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author  tejinder
 *  This class provides database services to get a Connection.
 *  It is designed to be a singleton, the constructor is private and the getInstance method
 *  must be used.
 */
public class DataBaseManager{

    //TODO: to implement 
    /**
    * This method is uses to retrieve the name of the pool of connections for the application.
    * @return connection pool name
    */

   /// private String getPoolName() {
    //}

   /**
    * This method retrieves a connection from the database
    * @return database connection
    * @throws EPlatformDatabaseException if there is an error getting a Connection
    */
    public static Connection getConnection() throws EPlatformDatabaseException {

            //TODO : connection pooling
            //DriverManager.getConnection(getPoolName());
            Connection con = null;

		try {
			Class.forName("com.mysql.jdbc.Driver");
                        //there is no port number?
			con =  DriverManager.getConnection("jdbc:mysql://localhost/digistore","root","766937");
		} catch (ClassNotFoundException e) {
			throw new EPlatformDatabaseException("");
		} catch (SQLException e) {
			throw new EPlatformDatabaseException("");
		}
		return con;
	}


	/**
	 * This method is responsible to close the connection instances.
	 * @param con
	 * @throws EPlatformDatabaseException
	 */
	public static void closeConnection(Connection con) throws EPlatformDatabaseException{
		try {
			if(con != null){
				con.close();
			}
		} catch (SQLException e) {
			throw new EPlatformDatabaseException("");
		}
	}

	/**
	 * This method is responsible to close the ResultSet instances.
	 * @param resultSet
	 * @throws EPlatformDatabaseException
	 */
	public static void closeResultset(ResultSet resultSet) throws EPlatformDatabaseException{
		try {
			if(resultSet != null){
				resultSet.close();
			}

		} catch (SQLException e) {
			throw new EPlatformDatabaseException("");
		}

	}

	/**
	 * This method is responsible to close the Statement instances.
	 * @param prepareStatement
	 * @throws EPlatformDatabaseException
	 */
	public static void closePreparedStatement(PreparedStatement prepareStatement) throws EPlatformDatabaseException{
		try {
			if(prepareStatement != null){
				prepareStatement.close();
			}

		} catch (SQLException e) {
			throw new EPlatformDatabaseException("");
		}

	}

        //  TODO: to implement
    public Long getSequenceValue(String sequenceName) throws EPlatformDatabaseException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
